package com.mew.lobby.config;

import io.netty.channel.Channel;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class LobbySessionManager {
    private static LobbySessionManager sessionManager = new LobbySessionManager();

     private ConcurrentHashMap<Long, LobbySession> sessionMap = new ConcurrentHashMap(4);

    private LobbySessionManager() {
    }

    public static LobbySessionManager getInstance() {
        if (null == sessionManager) {
            sessionManager = new LobbySessionManager();
        }
        return sessionManager;
    }

    public LobbySession getSession(long userId){
        return sessionMap.get(userId);
    }

    public void putSession(LobbySession session){
        sessionMap.put(session.getUserId(), session);
    }

    public LobbySession removeSession(long userId){
        return sessionMap.remove(userId);
    }

    public List<LobbySession> findByChannel(Channel channel){
        return sessionMap.values().stream().filter(ls -> channel.equals(ls.getChannel())).collect(Collectors.toList());
    }

    public long size(){
        return sessionMap.size();
    }


}
